<?php
/*
    GNU/Linux Activation Technologies Key Server
    Copyright (C) 2019 GNU/Linux Activation Technologies

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

if (! isset($_GET["hwid"])) {
	exit("Missing hardware ID.");
}

if (! isset($_GET["product_key"])) {
	exit("Missing product key.");
}

$mysqli = new mysqli("host", "username", "password", "database");
if ($mysqli->connect_errno) {
    exit("FAIL"); // server is broken
}

$stmt = $mysqli->prepare("SELECT hwid, blacklisted FROM licenses WHERE product_key = ?");
$stmt->bind_param('s', $_GET["product_key"]);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 1) {
	while($row = $result->fetch_assoc()) {
		if ($row["blacklisted"]) {
			exit("FAIL"); // product key blacklisted
		}
        
        if (strcmp($_GET["hwid"], $row["hwid"])) {
			$stmt = $mysqli->prepare("UPDATE licenses SET hwid = ? WHERE product_key = ?");
			$stmt->bind_param('ss', $_GET["hwid"], $_GET["product_key"]);
			$stmt->execute();
		}    
	}
	exit("OK"); // product key valid
} else {
	exit("FAIL"); // product key invalid or server is broken
}
?>
